<?php

class Websis_Controller_Action extends Zend_Controller_Action
{
    public function indexAction()
    {
        $model = $this->_getModel();

        $rowSet = $model->fetchAll(null, $model->getCampoOrdenacao());
        $this->view->rowSet = $rowSet;
    }

    public function formularioAction()
    {
        $model = $this->_getModel();

        if($id = $this->_getParam('id')){
            $row = $model->find($id)->current();
        } else {
            $row = $model->createRow();
        }

        $this->view->row = $row;
    }

    public function gravarAction()
    {
        $dados = $this->_getAllParams();
        $model = $this->_getModel();
        $id = $model->gravar($dados);

        $this->_redirect($dados['controller'] . '/index');
    }

    public function excluirAction()
    {
        $dados = $this->_getAllParams();

        $model = $this->_getModel();
        $model->excluir($this->_getParam('id'));

        $this->_redirect($dados['controller'] . '/index');
    }

    protected function _getModel()
    {
        $_model = 'Application_Model_' . str_replace('Controller', '', get_class($this));
        return new $_model;
    }
}
